package guoanmaker.operator.business.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.modelnobind.LuckdrawRecordAndCoupons;

@Repository
public class LuckdrawRecordAndCouponsDao {
	@PersistenceContext
	private EntityManager manager;
	public List<LuckdrawRecordAndCoupons> queryLuckRecordAndCouponsDao(String userId,String activityId){
		String sql = "SELECT a.id, a.luckdraw_type, a.record_time, ol.pic_url, oc.couponsname, CONCAT('(满 ', oc.fullamount, ' 减 ', oc.subamount, ')') AS dfn_name"
				+ " FROM t_operator_luckdraw_record a LEFT JOIN t_operator_coupons oc ON a.coupons_id = oc.id, t_operator_luckdraw ol"
				+ " WHERE to_days(a.record_time) = to_days(now()) AND ol.id = a.luckdraw_id AND a.activity_id =:activityId AND a.user_id =:userId"
				+ " ORDER BY a.record_time DESC ";
		Query query = manager.createNativeQuery(sql, LuckdrawRecordAndCoupons.class);
		query.setParameter("userId", userId);
		query.setParameter("activityId", activityId);
		List<LuckdrawRecordAndCoupons> enlist = (List<LuckdrawRecordAndCoupons>) query.getResultList();
		return enlist;
	}
	
	public List<LuckdrawRecordAndCoupons> queryAllLuckRecordAndCouponsDao(String userId,String activityId){
		String sql = "SELECT a.id, a.luckdraw_type, a.record_time, ol.pic_url, oc.couponsname, CONCAT('(满 ', oc.fullamount, ' 减 ', oc.subamount, ')') AS dfn_name"
				+ " FROM t_operator_luckdraw_record a LEFT JOIN t_operator_coupons oc ON a.coupons_id = oc.id, t_operator_luckdraw ol"
				+ " WHERE ol.id = a.luckdraw_id AND a.activity_id =:activityId AND a.user_id =:userId"
				+ " ORDER BY a.record_time DESC ";
		Query query = manager.createNativeQuery(sql, LuckdrawRecordAndCoupons.class);
		query.setParameter("userId", userId);
		query.setParameter("activityId", activityId);
		List<LuckdrawRecordAndCoupons> enlist = (List<LuckdrawRecordAndCoupons>) query.getResultList();
		return enlist;
	}
}
